package com.cm.sys.system.controller;

import java.util.List;
import java.util.Map;

import com.cm.sys.framework.aop.NotCheckIP;
import com.cm.sys.framework.aop.NotCheckLogin;
import com.cm.sys.framework.utils.JwtUtils;
import com.cm.sys.system.entity.BusinessInfo;
import com.cm.sys.system.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.application.RequestParameter;
import com.cm.sys.framework.application.ResponseResult;
import com.cm.sys.framework.execption.BusinessException;
import com.cm.sys.framework.utils.JsonUtil;
import com.cm.sys.system.entity.BusinessTree;
import com.cm.sys.system.entity.Menu;
import com.cm.sys.system.enums.ApprovalStatus;
import com.cm.sys.system.service.BusinessService;
import com.cm.sys.system.service.MenuService;

/***
 * 业务线管理接口
 * 
 * @author yxq18
 * @date 2019/12/17
 */
@Controller
public class BusinessController {

    private static final Logger logger = LoggerFactory.getLogger(BusinessController.class);

    @Autowired
    private BusinessService businessService;

    @Autowired
    private MenuService menuService;

    /***
     * 新建业务线接口
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/saveBusiness", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveBusinessInfo(RequestParameter rp) {

        JSONObject args = rp.getparameters();

        JSONObject menu = args.getJSONObject("menu");

        Menu returnMenu = businessService.saveMenu(menu);

        JSONArray businessArray = args.getJSONArray("business");

        logger.info("menu数据===========【{}】============", menu);
        logger.info("business数据================【{}】===============", businessArray);

        saveBusiness(businessArray, returnMenu);
        JSONObject condition = args.getJSONObject("condition");
        businessService.saveCondition(condition, returnMenu.getId());
        ResponseResult rr = new ResponseResult();
        return rr;

    }

    /***
     * 业务线列表查询接口
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/businessList", method = RequestMethod.POST)
    public ResponseResult businessList(RequestParameter rp) {

        JSONObject args = rp.getparameters();

        Integer pageIndex = JsonUtil.getInt(args, "pageIndex");

        Integer pageSize = JsonUtil.getInt(args, "pageSize");

        String search = JsonUtil.getString(args, "search", null);

        ResponseResult rr = new ResponseResult();

        logger.info("业务线列表查询接口入参===========【{}】============", args);

        Map<String, Object> map = businessService.selectBusiness(pageIndex, pageSize, search);

        rr.setMap(map);
        return rr;

    }

    /***
     * 查询业务详情接口
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/businessInfo", method = RequestMethod.POST)
    public ResponseResult businessListInfo(RequestParameter rp) {
        JSONObject args = rp.getparameters();
        Integer menuId = JsonUtil.getInt(args, "id");
        ResponseResult rr = new ResponseResult();
        logger.info("查询业务详情接口入参===========【{}】============", args);
        List<BusinessTree> list = businessService.selectBusinessInfo(menuId);
        rr.set("lists", list);
        return rr;

    }

    /***
     * 更新审核状态的接口
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/verifyBusiness", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateBusiness(RequestParameter rp) {
        JSONObject args = rp.getparameters();
        Integer menuId = JsonUtil.getInt(args, "id");
        // 1表示审核中 2表示通过 3表示拒绝
        Integer reviewStatus = JsonUtil.getInt(args, "reviewStatus");

        logger.info("更新审核状态的接口入参数=======【{}】============", args);

        if (reviewStatus.equals(ApprovalStatus.SUCCESS.getCode())
            || reviewStatus.equals(ApprovalStatus.REJECT.getCode())) {

            Menu menu = menuService.selectMenu(menuId);
            if (!StringUtils.isEmpty(menu)) {
                if (menu.getReviewStatus().equals(ApprovalStatus.UNDER_REVIEW.getCode())) {
                    menu.setReviewStatus(reviewStatus);
                    menuService.updaterStatus(menu);
                } else {
                    throw new BusinessException(ApiResponseCode.DEL_FAIL.getCode(), "已经被处理,不能重复处理");
                }
            }
        } else {
            throw new BusinessException(ApiResponseCode.DEL_FAIL.getCode(), "reviewStatus值不合法");
        }
        ResponseResult rr = new ResponseResult();

        return rr;

    }

    /***
     * 处理数据前端转过来的数据
     * 
     * @param businessArray
     * @param returnMenu
     */
    private void saveBusiness(JSONArray businessArray, Menu returnMenu) {

        List<Map> lists = JSON.parseArray(businessArray.toJSONString(), Map.class);

        for (Map list : lists) {
            List<Map> moduleLists = (List<Map>)list.get("data");
            JSONObject moduleargs = new JSONObject();
            moduleargs.put("businessName", list.get("businessName").toString());
            moduleargs.put("menuId", returnMenu.getId());
            moduleargs.put("parentId", 0);
            BusinessInfo businessInfo = businessService.saveBusinessInfo(moduleargs);
            for (Map moduleList : moduleLists) {
                JSONObject modulearg = new JSONObject();
                modulearg.put("businessName", businessInfo.getBusinessName());
                modulearg.put("menuId", returnMenu.getId());
                modulearg.put("localhostAccount", moduleList.get("localhostAccount"));
                modulearg.put("localhostDirectory", moduleList.get("localhostDirectory"));
                modulearg.put("localhostIp", moduleList.get("localhostIp"));
                modulearg.put("localhostPassword", moduleList.get("localhostPassword"));
                modulearg.put("parentId", businessInfo.getBusinessId());
                businessService.saveBusinessInfo(modulearg);
            }
        }
    }





}
